package com.strato.repository.monitor.v1_0_0;

import java.io.File;
import java.io.InputStream;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Map;

import com.strato.repository.monitor.v1_0_0.timer.IntervalThread;

public abstract class AbstractFileMonitor extends IntervalThread implements FileMonitor{
	
	private Collection<FileListener> listeners = new LinkedList<FileListener>();
	
	public AbstractFileMonitor(int intervalSeconds, String name) {
		super(intervalSeconds, name);
	}

    public void addListener(FileListener listener) {
        listeners.add(listener);
    }

    public void removeListener(FileListener listener) {
        listeners.remove(listener);
    }
    
    protected void intervalStarted(){
        Iterator<FileListener> listIt = listeners.iterator();
        while (listIt.hasNext()) {
            listIt.next().onIntervalStart();
        }
    }
    
    protected void intervalEnded(Map<String,String> eventsToFile){
        Iterator<FileListener> listIt = listeners.iterator();
        while (listIt.hasNext()) {
            listIt.next().onIntervalEnd(eventsToFile);
        }
    }
    
    protected void fileAdded(String path,InputStream is) {
        Iterator<FileListener> listIt = listeners.iterator();
        while (listIt.hasNext()) {
            listIt.next().onAdd(path,is);
        }
    }

    protected void fileChanged(String path,InputStream is) {
        Iterator<FileListener> listIt = listeners.iterator();
        while (listIt.hasNext()) {
            ((FileListener) listIt.next()).onChange(path,is);
        }
    }

    protected void fileDeleted(String path) {
        Iterator<FileListener> listIt = listeners.iterator();
        while (listIt.hasNext()) {
            ((FileListener) listIt.next()).onDelete(path);
        }
    }

    protected void monitorStarted(File monitoredFile) {
        Iterator<FileListener> listIt = listeners.iterator();
        while (listIt.hasNext()) {
            ((FileListener) listIt.next()).onStart(monitoredFile);
        }
    }

    protected void monitorStopped(File notMonitoredFile) {
        Iterator<FileListener> listIt = listeners.iterator();
        while (listIt.hasNext()) {
            ((FileListener) listIt.next()).onStop(notMonitoredFile);
        }
    }
}
